package main

import (
	"log"
	"net/http"
)

type helloHandler struct {
}

func (m *helloHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	writer.Write([]byte("myhandler"))
}

type aboutHandler struct {
}

func (a *aboutHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	writer.Write([]byte("about handler"))
}

func welcome(writer http.ResponseWriter, request *http.Request) {
	writer.Write([]byte("welcome"))
}

// handle	: handle 请求, 如何接收请求
// process  : process请求, 如何处理,响应请求

// Handler 接口
func main() {

	// 创建server两种方式
	//server  := http.Server{
	//	Addr: "localhost:8080",
	//	Handler: nil, // default
	//}
	//
	//http.ListenAndServe(":8080", nil)

	// Handler接口, http 包默认的DefaultServeMux 多路复用器(路由)
	mh := new(helloHandler)
	server := http.Server{
		Addr:    "localhost:8080",
		Handler: nil,
	}

	http.Handle("/hello", mh)
	http.Handle("/about", new(aboutHandler))

	// http.HandleFunc 函数转换
	http.HandleFunc("/home", func(writer http.ResponseWriter, request *http.Request) {
		writer.Write([]byte("home"))
	})

	http.HandleFunc("/welcome", welcome)

	// 启动服务
	log.Fatalln(server.ListenAndServe())

}
